package com.soosync.servlet;

import java.util.HashMap;

import com.soosync.common.AbstractHttpServlet;
import com.soosync.common.BaseException;
import com.soosync.common.PropertyManager;
import com.soosync.common.SQLManager;

import org.json.JSONException;
import org.json.JSONObject;

public class UserServlet extends AbstractHttpServlet {

	private static final long serialVersionUID = 1L;

	public static HashMap<String, Integer> funcMap = new HashMap<String, Integer>();

	static {
		funcMap.put("login", new Integer(1));
		funcMap.put("register", new Integer(2));
		funcMap.put("getuserinfo", new Integer(3));
		funcMap.put("logout", new Integer(4));
	}

	protected void process() {
		try {
			String func = requestBox.getString("func");
			Integer idx = (Integer) funcMap.get(func);
			switch (idx.intValue()) {
			case 1:
				login();
				break;
			case 2:
				register();
				break;
			case 3:
				getUserInfo();
				break;
			case 4:
				logout();
				break;
			default:
				break;
			}
		} catch (JSONException e) {
		}
	}

	private void login() throws JSONException {
		JSONObject resultjo = new JSONObject();
		JSONObject userjo = new JSONObject();
		String email = requestBox.getString("email");
		String password = requestBox.getString("password");
		String code = "OK0000";
		Object[] obj = new Object[] { email, password };
		try {
			String query = PropertyManager.getProperty("user.login");
			userjo = (JSONObject) SQLManager.select("appSource", query, obj,userjo);
			if(userjo.isNull("id")){
				code = "ER0001";
				resultjo = null;
			}else{
				resultjo.put("login", userjo);
				session.setAttribute("suid",userjo.get("id"));
			}
		} catch (BaseException e) {
			code = "ER0000";
			resultjo.put("errorinfo", e.toString());
		}
		resultBox.put("data", resultjo);
		resultBox.put("code", code);
	}

	private void register() throws JSONException {
		String email = requestBox.getString("email");
		String username = requestBox.getString("username");
		String password = requestBox.getString("password");
		Object[] obj = new Object[] { email, username, password };
		try {
			String query = PropertyManager.getProperty("user.add");
			int resultInt = SQLManager.execute("appSource", query, obj);
			resultBox.put("error", false);
			resultBox.put("value", resultInt);
		} catch (BaseException e) {
			resultBox.put("error", true);
			resultBox.put("value", e.toString());
		}
	}

	private void getUserInfo() throws JSONException {
		JSONObject resultjo = new JSONObject();
		String id = requestBox.getString("id");
		Object[] obj = new Object[] { id };
		try {
			String query = PropertyManager.getProperty("user.info");
			resultjo = (JSONObject) SQLManager.select("appSource", query, obj,
					resultjo);
			resultjo.remove("passowrd");
			resultBox.put("error", false);
			resultBox.put("value", resultjo);
		} catch (BaseException e) {
			resultBox.put("error", true);
			resultBox.put("value", e.toString());
		}

	}
	
	private void logout() throws JSONException {
		session.removeAttribute("suid");
		resultBox.put("code", "OK0000");
	}
}